<template>
  <el-main>
    <div class="row mb-1">
      <div class="col">
        <el-button @click="click" type="primary">创建作业</el-button>
      </div>
    </div>
    <div class="row mb-1">
      <div class="col">
        <el-table :data="group" border height="65vh" style="width: 100%">
          <el-table-column prop="name" label="名称"></el-table-column>
          <el-table-column label="操作" class-name="text-right">
            <template #default="scope">
              <el-button size="default" type="danger" @click="del(scope.row.ID)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </el-main>
</template>

<script lang="ts" setup>
import study from "../../../mysql_interface/studycodemodel";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  asyncadmindelteamusergroup,
  asyncadminaddteamusergroup,
} from "../../../api/index";
import Studycodemodel from "../../../mysql_interface/studycodemodel";

const p = defineProps<{ team: Studycodemodel.Iteam, group: study.Iteamusergroup[], start: Function }>()


function click() {
  ElMessageBox.prompt("请输入名称", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    inputValidator: (msg) => {
      return !p.group.map((v) => v.name).includes(msg);
    },
    inputErrorMessage: "名称重复",
  })
    .then(({ value }) => {
      if (p.team.ID) {
        asyncadminaddteamusergroup({
          teamid: p.team.ID,
          name: value,
          description: "",
        })
          .then((e) => {
            ElMessage.success("添加成功");
            if (p.start) p.start();
          })
          .catch((e) => {
            ElMessage.success("添加失败");
          });
      }
    })
    .catch(() => { });
}

async function del(ID: number) {
  ElMessageBox.confirm("确定要删除？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(async () => {
      await asyncadmindelteamusergroup({ ID });
      if (p.start) p.start();
    })
    .catch(() => {
      ElMessage.error("删除失败");
    });
}


</script>
